// pages/new_group/new_group.js
var app = getApp()
var header = {
  'content-type': 'application/x-www-form-urlencoded',
  'cookie': "openid=" + app.globalData.openid
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    
    loadgroupName:'',
    describe:'',
    name:'',
    telephone:'',
    longitude:'',
    latitude:'',
    locationAddress:'',
    name: '',
    district_info:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    that.setData({
      district_info: JSON.parse(options.district_info)
    });

  },

  choose_tude:function(){
    let that = this;
    
    wx.chooseLocation({
      // latitude: <!<<longitude,
      // longitude: that.data.latitude,
      success(res_location) {
        console.log(res_location, "res_location");
        that.setData({
          // hasLocation: true,
          // location: formatLocation(res.longitude, res.latitude),
          locationAddress: res_location.address,
          longitude: res_location.longitude,
          latitude: res_location.latitude,
          name: res_location.name
        });
      }
    })
    //   },
    // });
  },


  formSubmit: function (e) {
    let that = this;
    //console.log(e, "e");
    let long_lati = e.detail.value.input_long_lati;
    let long_lati_arr = long_lati.split(",");

    if (e.detail.value.input_loadgroup == ""){
      wx.showModal({
        title:'提示',
        content:'路段名称不能为空',
        showCancel:false
      });

      return;
    }

    if (e.detail.value.textarea == "") {
      wx.showModal({
        title: '提示',
        content: '路段描述不能为空',
        showCancel: false
      });

      return;
    }

    if(long_lati_arr[0] == ""|| long_lati_arr[1] == "") {
      wx.showModal({
        title: '提示',
        content: '经度或者纬度不能为空',
        showCancel: false
      });

      return;
    }

    //input_loadgroup: "邗江路江阳西路交叉口"    input_name: "江阳西路与邗江中路交叉口"    input_telephone: "经度:119.4012;纬度:32.36749"    textarea: "邗江路江阳西路"
    that.setData({      
      loadgroupName: e.detail.value.input_loadgroup,
      describe: e.detail.value.textarea,
      name: e.detail.value.input_name,
      telephone: e.detail.value.input_telephone,
      longitude: long_lati_arr[0],
      latitude: long_lati_arr[1]     
    });

    wx.request({
      url: 'https://119.3.46.145/server/addGroup?userid=' + app.globalData.userid + '&name=' + that.data.loadgroupName + '&description=' + that.data.describe + "&admin=" + that.data.name + '&phone=' + that.data.telephone + '&departmentID=' + that.data.district_info.mid + '&Longitude=' + that.data.longitude + '&Latitude=' + that.data.latitude,
      method: 'POST',
      header: header,
      success: res => {
        //data: { error: "0", info: "添加成功" }  {error: "-1", info: "已存在，无法添加"}
        //console.log(res.data, "data");
        let result = res.data;

        if (result.error == "0") {
          wx.showModal({
            title: '提示',
            content: result.info,
            success: function () {
              wx.request({
                url: 'https://119.3.46.145/server/loadTree?userid=' + app.globalData.userid + '&id=' + that.data.district_info.id,  //获取区域列表构造树
                header: header,
                success: function (res) {                 
                  let data = JSON.parse(res.data.data);
                 
                  if (data.length == 0) {
                    return;
                  }
               

                  let pages = getCurrentPages(); //获取上一个页面信息栈(a页面)
                  let prevPage = pages[pages.length - 2] //给上一页面的longitude latitude赋值
                  prevPage.setData({
                    loadgroup_list: data
                  });
                  wx.navigateBack(); //关闭当前页面，返回上一个页面
                },
                fail: function (error) {
                  console.log(error, "dev_manager  onload loadTree");
                }
              });
            }
          });
        }

        if(result.error == "-1"){
          wx.showModal({
            title: '提示',
            content: result.info,
            showCancel:false
          })
        }
      },
      fail: function (error) {
        console.log(error, "addDepartment error");
      }
    })
  },

  formReset: function (e) {
    let that = this;
    that.setData({
      loadgroupName: '',
      describe: '',
      name: '',
      telephone: '',
      longitude: '',
      latitude: '',
    });
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})